using Serilog;
using Serilog.Events;
using WorkerApp;

const string LogTemplate = "{SourceContext} {Timestamp:HH:mm:ss} [{Level:u3}] {Scope} {Message:lj} {NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
    .MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning)
    .MinimumLevel.Override("System.Net.Http", LogEventLevel.Warning)
    .Enrich.WithProperty("Application", "WorkerApp")
    .WriteTo.Console(outputTemplate: LogTemplate)
    .WriteTo.File("Logs/log.txt", rollingInterval: RollingInterval.Day, outputTemplate: LogTemplate)
    .CreateLogger();

Log.Information("Starting up");

try
{
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddSerilog();

    builder.Services.AddMiniJobs(options =>
    {
        options.AppName = "WorkerApp";
        options.ServerAddress = "http://localhost:5032";
        options.Port = 5006;
        options.Jobs.RegisterJob<DemoJob>();
    });

    var app = builder.Build();

    app.MapJobsHandlers();

    app.Run();
    return 0;
}
catch (Exception ex)
{
    Log.Fatal(ex, "Unhandled exception");
    return 1;
}
finally
{
    await Log.CloseAndFlushAsync();
}
